export default {
    data() {
        return {
            noOpenIdFlag: false
        }
    },
	computed: {
		noticeTemplate() {
			return this.$store.state.noticeTemplate
		},
	},
    onLoad() {
        if (!this.$store.openId) { // 没有openId时先从本地取
            let storeOpenId = uni.getStorageSync('openId');
            if (storeOpenId) {
                this.$store.commit('setOpenId', storeOpenId);
            }
		}
		
		// #ifdef MP || MP-WEIXIN
        if (!this.$store.state.miniAppId) {
            const appId = uni.getAccountInfoSync().miniProgram.appId
            console.log('当前小程序的appId', appId)
            this.$store.commit('setMiniAppId', appId)
		}
		// #endif
    },
    methods: {
		// 检查上传是否配置
		validShopSetting() {
			const shopUrl = uni.getStorageSync('shopUrl')
			if (!shopUrl) {
				uni.showToast({
					icon: 'none',
					title: '商城地址错误，请联系管理处。',
				});
			}
			return !!shopUrl
		},
        confirmLogin() {
            this.noOpenIdFlag = false
            // uni.clearStorage()
            // uni.clearStorageSync()
            this.$store.commit('setTopServices', [])
            this.$store.commit('setAllServiceList', [])
            this.$store.commit('setOpenId', '')
            this.$Router.replace({name: 'login'})
        },
        modalClose() {
            this.noOpenIdFlag = false
        },
		// 把编码转换id
		transformCodeToId(arrCode) {
			const output = this.noticeTemplate.filter(v => arrCode.includes(v.templateCode))
			return output.map(v => v.templateId)
		},
		// 设置小程序的消息推送
		setMiniNotice(chatMiniPage) {
			const arrTemplate = this.noticeTemplate.filter(v => v.chatMiniPage.split(',').includes(chatMiniPage))
			if (arrTemplate.length === 0) {
				return
			}
			
			const tmplIds = arrTemplate.map(v => v.templateId)
			return new Promise((resolve, reject) => {
				wx.requestSubscribeMessage({
					tmplIds,
					success: (res) => {
						if (res.errMsg !== 'requestSubscribeMessage:ok') {
							return
						}

						console.log('saveTemplateSetting:=====')
						let acceptArr = []
						arrTemplate.forEach(v => {
							if (res[v.templateId] === 'accept') {
								acceptArr.push(v)
							}
						})
						console.log(res)
						console.log(this.noticeTemplate)
						console.log(tmplIds)
						this.$Posting(this.$api.chatTemplateAuthRecord, {
							tenantId: this.projectInfo.tenantId,
							authList: acceptArr
						})
						resolve()
					},
					fail(err) {
						console.log(err)
						resolve()
					}
				})
			})
		}
    }
}